/**
 * Created with JetBrains PhpStorm.
 * User: duonglt
 * Date: 9/20/13
 * Time: 11:10 AM
 * To change this template use File | Settings | File Templates.
 */
(function($) {

    $.fn.lov = function( options ) {

        // Establish our default settings
        var settings = $.extend({
            dataSource    : null,
            name    : null
        }, options);
        var rdmStr = $.randomString();

        return this.each( function() {
            window['me'+rdmStr]  = $(this);
            $(this).after('&nbsp;&nbsp;<a id="btnLov_'+rdmStr+'" class="k-button k-button-icontext k-grid-edit" >...</a>' +
                '<div id="wndChooser'+rdmStr+'" style="display: none">'+
                '<input id="txtSearch'+rdmStr+'" style="width: 480px" />'+
                '<div id="gridAllData'+rdmStr+'" style="margin: 10px 0px"></div>'+
                '</div>');



            var wndDataChooser = $('#wndChooser'+rdmStr)
                .kendoWindow({
                    title: "Chọn "+settings.name,
                    modal: true,
                    visible: false,
                    resizable: false,
                    width: 500,
                    activate: onActivateChooseData

                }).data("kendoWindow");

            $('#txtSearch'+rdmStr).kendoAutoComplete({
                dataSource:{
                    transport:{
                        read:{
                            url:'/common/index/lov-'+settings.dataSource,
                            dataType:'json',
                            type: "POST"

                        }
                    },
                    schema: {
                        data: function(data) {
                            return data.data;
                        }
                    }
                },
                filter: "startswith",
//                minLength: 2,
                placeholder: "Nhập tên "+settings.name+" để tìm kiếm",
                separator: "",
                dataValueField: "id",
                dataTextField: "name",
                dataBound: function(e) {
                    value = this.value();
                    filter = [];
                    grid = $("#gridAllData" +rdmStr).data("kendoGrid");
                    if (value) {
                        filter.push({ field:"name", operator:"startswith", value:value});

                        grid.dataSource.filter(filter);
                    }
                },
                close: function(e) {
                    grid = $("#gridAllData" +rdmStr).data("kendoGrid");
                    grid.dataSource.filter([]);
                },
                change: function(e) {
                    item  = this._current;
                    dataItem = this.dataItem(item.index());

                    wndDataChooser.close();
                    me.val(dataItem.id);
                    me.trigger('change');
                }
            });

            $("#gridAllData"+rdmStr).kendoGrid({
                dataSource: {
                    transport:{
                        read:{
                            url:'/common/index/lov-'+settings.dataSource,
                            dataType:'json',
                            type: "POST"

                        }
                    },
                    schema: {
                        data: function(data) {
                            return data.data;
                        },
                        model: {
                            fields: {
                                name: { type: "string" },
                                id: { type: "string" }
                            }
                        },
                        total: function(result) {
                            data = this.data(result);
                            return data ? data.length : 0;
                        }
                    },
                    pageSize: 10
                },
                navigatable: true,
                pageable: true,
                sortable: true,
                selectable:'single',
                columns: [
                    { field: "name", title: "Tên", width: 100 }
                ]
            });

            $("#gridAllData"+rdmStr).delegate("tbody>tr", "dblclick", function(){
                grid = $("#gridAllData"+rdmStr).data("kendoGrid");
                dataItem = grid.dataItem(grid.select());
                wndDataChooser.close();
                window['me'+rdmStr] .val(dataItem.id);
                window['me'+rdmStr] .trigger('change');
            });

            $("#btnLov_"+rdmStr).click(function(){
                wndDataChooser.center().open();
            });

            function onActivateChooseData(){
                $("#txtSearch"+ rdmStr).val('');
                $("#txtSearch"+ rdmStr).focus();
            }
        });

    }




}(jQuery));